package com.example.pceb.handler;

import com.example.pceb.result.Result;
import com.example.pceb.result.ResultCode;
import io.jsonwebtoken.JwtException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> handleException(Exception e) {
        return Result.fail(e.getMessage(), ResultCode.FAIL);
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result<Object> handleException(MissingServletRequestParameterException e) {
        return Result.fail("缺少参数"+e.getParameterName(), ResultCode.FAIL);
    }
    @ExceptionHandler(JwtException.class)
    @ResponseBody
    public Result<Object> handleJwtException(JwtException e) {
        return Result.fail("JWT验证失败：" + e.getMessage(), ResultCode.FAIL);
    }

}
